package com.zhouxiaoge.controller;

import com.zhouxiaoge.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @author 周明阳
 * mail:gqzmy@outlook.com
 * 时间:2018-03-20 16:45
 */

@RestController
public class MovieController {

    @Autowired
    @Qualifier("remoteRestTemplate")
    private RestTemplate remoteRestTemplate;

    @Autowired
    private LoadBalancerClient loadBalancerClient;

    @GetMapping("/movie/{id}")
    public User findById(@PathVariable Long id) {
        return this.remoteRestTemplate.getForObject("http://user-provider-1/simple/" + id, User.class);
    }

    @GetMapping("/")
    public String loadBalanced() {
        ServiceInstance serviceInstance = this.loadBalancerClient.choose("user-provider-1");
        String result = serviceInstance.getUri() + ": " + serviceInstance.getHost() + ": " + serviceInstance.getServiceId();
        System.out.println(result);
        return result;
    }
}
